import {request} from "@umijs/max";

/**
 * 查询api列表
 * @param name
 * @param options
 */
export async function queryOpenApiForGet(name: string, options?: { [key: string]: any }) {
  return request<ResponseMessage<PagerResult<OpenApiEntity[]>>>('/openapi/details', {
    method: 'GET',
    params: {
      name
    },
    ...(options || {}),
  });
}


/**
 * 查询api列表
 * @param id
 * @param options
 */
export async function queryOpenApiById(id: string, options?: { [key: string]: any }) {
  return request<ResponseMessage<OpenApiEntity>>(`/openapi/detail/${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 上传单个文件
 * @param file
 * @param options
 */
export async function protocolFileUpload(file: any, options?: { [key: string]: any }) {
  let formData = new FormData()
  formData.append('file', file)
  return request<ResponseMessage<string>>('/openapi/upload', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: formData,
    ...(options || {}),
  });
}

/**
 * 添加api协议
 * @param file
 * @param options
 */
export async function addProtocolByJar(data: any, options?: { [key: string]: any }) {
  return request<ResponseMessage<string>>('/openapi-protocol/load', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data,
    ...(options || {}),
  });
}

/**
 * 删除api协议
 * @param file
 * @param options
 */
export async function deleteProtocolById(id: string, options?: { [key: string]: any }) {
  return request<ResponseMessage<string>>(`/openapi-protocol/remove/${id}`, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
    },
    ...(options || {}),
  });
}
